﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace Worktool
{
    public partial class Login : Form
    {
        private string password; // User Eingabepassword
        private string dbPassword; // Password from DB
        public Loading loadscreen = new Loading();
            
        
        public Login()
        {
            InitializeComponent();
            this.Text = "Login: " + Properties.Settings.Default.CurrentDBName; 
            this.tbl_userTableAdapter.Fill(this.aSCDataSet.tbl_user);
            label3.Text = Properties.Settings.Default.CurrentDBName;

            // Disable all Modules

            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            button7.Enabled = false;
            button8.Enabled = false;
            button9.Enabled = false;
            button10.Enabled = false;
            button11.Enabled = false;
            button12.Enabled = false;

            button1.BackColor = Color.Gray;
            button2.BackColor = Color.Gray;
            button3.BackColor = Color.Gray;
            button4.BackColor = Color.Gray;
            button5.BackColor = Color.Gray;
            button6.BackColor = Color.Gray;
            button7.BackColor = Color.Gray;
            button8.BackColor = Color.Gray;
            button9.BackColor = Color.Gray;
            button10.BackColor = Color.Gray;
            button11.BackColor = Color.Gray;
            button12.BackColor = Color.Gray;


        }

        public void Login_Load(object sender, EventArgs e)
        {
            loadscreen.Hide();
            try
            {
                if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["changePW"]) == true)
                {
                    label2.Text = "Please insert a new password";
                    label2.ForeColor = Color.Red;
                }
                else
                {
                    label2.Text = "Enter password";
                    label2.ForeColor = Color.Black;
                }

            }
            catch (Exception ex)
            {
            }
        }


        public void pwTest()
        {

            bool change;
            password = textBox1.Text;
            change = Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["changePW"]);
            if ((change == true) && (textBox1.Text != ""))
            {
                
                aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["password"] = CalculateMD5Hash(password);
                aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["changePW"] = false;
                tbl_userTableAdapter.Update(aSCDataSet);
                Properties.Settings.Default.User = comboBox1.Text;
                Properties.Settings.Default.Save();
                startUI();
            }
            else
            {
                dbPassword = comboBox1.SelectedValue.ToString();
                if (dbPassword == CalculateMD5Hash(password))
                {
                    Properties.Settings.Default.User = comboBox1.Text;
                    Properties.Settings.Default.Save();
                    startUI();
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Falsches Passwort. Bitte versuchen Sie es noch einmal");
                }
            }


        }

        public string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                pwTest();
                comboBox1.Focus();
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter && textBox1.Text != "")
            {
                pwTest();
                comboBox1.Focus();
            }
        }

        //
        // UI je nach Zugriffsberechtigung herstellen
        //
        public void startUI()
        {
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modSerials"]) == true)
            {
                button1.BackColor = Color.DarkSeaGreen;
                button1.Enabled = true; // Serials

                
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modComm"]) == true)
            {
                button2.BackColor = Color.DarkSeaGreen;
                button2.Enabled = true; // Commissioning
                
                
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modIp"]) == true)
            {
                button3.BackColor = Color.SteelBlue;
                button3.Enabled = true; 

               
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modRacks"]) == true)
            {
                button7.BackColor = Color.BurlyWood;
                button7.Enabled = true; 
                
               
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modRooms"]) == true)
            {
                button8.BackColor = Color.BurlyWood;
                button8.Enabled = true; 
               
               
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modStammdaten"]) == true)
            {
                button4.BackColor = Color.SteelBlue;
                button4.Enabled = true; 

               
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modDmx"]) == true)
            {
                button5.BackColor = Color.SteelBlue;
                button5.Enabled = true; 

                
               
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modDelivery"]) == true)
            {
                button9.BackColor = Color.BurlyWood;
                button9.Enabled = true;
                
                
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modProv"]) == true)
            {
                button10.BackColor = Color.BurlyWood;
                button10.Enabled = true; 
               
               
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modMatDetail"]) == true)
            {
                button6.BackColor = Color.SteelBlue;
                button6.Enabled = true; 
                
               
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modBaufortschritt"]) == true)
            {
                button11.BackColor = Color.Plum;
                button11.Enabled = true; 
                
               
            }
            if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["modLieferkette"]) == true)
            {
                button12.BackColor = Color.DarkSeaGreen;
                button12.Enabled = true; 
                
               
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = "";
            try
            {
                if (Convert.ToBoolean(aSCDataSet.tbl_user.Rows[comboBox1.SelectedIndex]["changePW"]) == true)
                {
                    label2.Text = "Please insert a new password";
                    label2.ForeColor = Color.Red;
                }
                else
                {
                    label2.Text = "Enter password";
                    label2.ForeColor = Color.Black;
                }

            }
            catch (Exception ex)
            { 
            }

        }

        //
        // Modul Seriennummern öffnen
        //
        private void button1_Click(object sender, EventArgs e)
        {
            Serials serials = new Serials();
            serials.Show();
        }
        
        //
        // Modul Commissioning öffnen
        //
        private void button2_Click(object sender, EventArgs e)
        {
            Commissioning comm = new Commissioning();
            comm.Show();
        }

        //
        // Modul IpDevices öffnen
        //
        private void button3_Click(object sender, EventArgs e)
        {
            IpDevices ipDevices = new IpDevices();
            ipDevices.Show();
        }

        //
        // Modul Stammdaten öffnen
        //
        private void button4_Click(object sender, EventArgs e)
        {
            Stammdaten stamm = new Stammdaten();
            stamm.Show();            
        }

        //
        // Modul DMX öffnen
        //
        private void button5_Click(object sender, EventArgs e)
        {
            DmxDevices dmxDevices = new DmxDevices();
            dmxDevices.Show();
        }

        //
        // Modul Material Detail öffnen
        //
        private void button6_Click(object sender, EventArgs e)
        {
            MaterialDetail matDetail = new MaterialDetail();
            matDetail.Show();
        }

        //
        // Modul Rackcontent Report öffnen
        //
        private void button7_Click(object sender, EventArgs e)
        {
            ReportRacks ReportRacks = new ReportRacks();
            ReportRacks.Show();
        }

        //
        // Modul Roomcontent Report öffnen
        //
        private void button8_Click(object sender, EventArgs e)
        {
            ReportRooms ReportRooms = new ReportRooms();
            ReportRooms.Show();
        }

        //
        // Modul Liefertermine öffnen
        //
        private void button9_Click(object sender, EventArgs e)
        {
            ReportLiefertmine liefertermine = new ReportLiefertmine();
            liefertermine.Show();
        }

        //
        // Modul Bereitstellungstermine öffnen
        //
        private void button10_Click(object sender, EventArgs e)
        {
            ReportBedarfstermine bedarf = new ReportBedarfstermine();
            bedarf.Show();
        }

        //
        // Modul Baufortschritt öffnen
        //
        private void button11_Click(object sender, EventArgs e)
        {
            Baufortschritt bf = new Baufortschritt();
            bf.Show();
        }

        //
        // Modul Lieferkette öffnen
        //
       
        private void button12_Click(object sender, EventArgs e)
        {
            DeliveryChain dc = new DeliveryChain();
            dc.Show();
        }
    }
}
